package com.topfinance.cnaps2.web.config;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.topfinance.frame.util.StringUtil;
import com.topfinance.frame.web.BaseSupport;
import com.topfinance.payment.ebo.TCfgFormatEbo;
import com.topfinance.payment.service.ITCfgFormatMgr;
import com.opensymphony.xwork2.ModelDriven;

/**
 * <p>
 * Description:
 * </p>
 * 
 * @author
 * @version 1.50
 *          <p>
 *          Copyright: Copyright (c) 2004 2005
 *          </p>
 *          <p>
 *          Company:
 *          </p>
 */
public final class CfgFormatAction extends BaseSupport implements ModelDriven<TCfgFormatEbo> {

	/**
	 * 
	 */
	private static final long serialVersionUID = -4650749921776542075L;

	private ITCfgFormatMgr formatManager = null;

	private TCfgFormatEbo formatEbo = new TCfgFormatEbo();

	public TCfgFormatEbo getModel() {
		return formatEbo;
	}

	public void setFormatManager(ITCfgFormatMgr formatManager) {
		this.formatManager = formatManager;
	}

	public CfgFormatAction() {
		super();
	}

	public String execute() {
		return list();
	}

	public String list() {
		log.debug("CfgTransAction-list begin");
		start1 = pageNum==1?0:(pageNum-1)* numPerPage;
		limit1 = pageNum*numPerPage;
		setCurrentPage(pageNum);
		try {
			dataList = formatManager.getAllFormat();
			countitem = dataList.size();
		} catch (Exception e) {
			handleException(e);
		}
		log.debug("CfgTransAction-list end");
		return SUCCESS;
	}
	
	public String liststruct() {
		log.debug("CfgTransAction-struct begin");
		try {
			List l = StringUtil.mapClass(TCfgFormatEbo.class);
			log.debug("[ConfNodeEbo Struct]" + l.size());
			rtnResultMap = new HashMap();
			rtnResultMap.put(DATA_LIST_ROOT, l);
			rtnResultMap.put(RECORDS_COUNT, l.size());
		} catch (Exception e) {
			handleException(e);
		}
		log.debug("CfgTransAction-struct end");
		return SUCCESS;
	}

	public String create() {
		try {
			log.debug(StringUtil.mapObject(formatEbo));
			formatManager.addFormat(formatEbo);
			rtnResultMap = new HashMap();
			rtnResultMap.put("success", "true");
			String[] args = new String[1];
			args[0] = "" + formatEbo.getOid();
			rtnResultMap.put("info", getText(CREATE_SUCCESS_KEY, args));
		} catch (Exception e) {
			handleException(e);
		}
		return SUCCESS;
	}

	public String update() {
		try {
			log.debug(StringUtil.mapObject(formatEbo));
			formatManager.editFormat(formatEbo);
			rtnResultMap = new HashMap();
			rtnResultMap.put("success", "true");
			String[] args = new String[1];
			args[0] = "";// you can add extra message here.
			rtnResultMap.put("info", getText(UPDATE_SUCCESS_KEY, args));
		} catch (Exception e) {
			handleException(e);
		}
		return SUCCESS;
	}

	public TCfgFormatEbo getFormatEbo() {
		return formatEbo;
	}

	public void setFormatEbo(TCfgFormatEbo formatEbo) {
		this.formatEbo = formatEbo;
	}

	public String query() {
		if (request.getParameter("id") == null) {
			rtnResultMap = new HashMap();
			String[] args = new String[1];
			args[0] = "";// you can add extra message here
			rtnResultMap.put("info", getText("expect.valid.pk.data", args));
			return SUCCESS;
		}
		try {
			String uid = request.getParameter("id");
			TCfgFormatEbo trans = formatManager.getTCfgFormat(new Integer(uid));
			List l = new ArrayList<TCfgFormatEbo>();
			l.add(trans);
			rtnResultMap = new HashMap();
			rtnResultMap.put(DATA_LIST_ROOT, l);
			rtnResultMap.put(RECORDS_COUNT, 1);
		} catch (Exception e) {
			handleException(e);
		}
		return SUCCESS;
	}
	
	public String delete() {
		try {
			log.debug(StringUtil.mapObject(formatEbo));
			String delId = request.getParameter("id");
			System.out.println("============================"+delId);
			formatManager.deleteTCfgFormat(new Integer(delId));
			rtnResultMap = new HashMap();
			rtnResultMap.put("success", "true");
			String[] args = new String[1];
			args[0] = "";// you can add extra message here.
			rtnResultMap.put("info", getText(DELETE_SUCCESS_KEY, args));
		} catch (Exception e) {
			handleException(e);
		}
		return SUCCESS;
	}	
	
	public void test() {
		 
	}
	
}
